package com.yishun.springboot.httpclient;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yishun.springboot.pojo.GenOneDataResults;
import com.yishun.springboot.pojo.GeneralRequestModel;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Post8806 {

//	POST有参(对象参数)：  通用模板6620

	public void doPostTestTwo() {

		// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();

		// 创建Post请求
		HttpPost httpPost = new HttpPost("http://47.99.64.49:2000/Yc/GetData");
		GeneralRequestModel generalModel = new GeneralRequestModel();
		generalModel.setProjectId("6620");
		generalModel.setDeviceCode("000000YKDZHJJC0000000420");
		generalModel.setBeginTime("2020-11-11 12:11:15");
		generalModel.setEndTime("2020-11-21 13:11:15");
		generalModel.setPageNumber(2);
		generalModel.setPageNumber(20);
		// 我这里利用阿里的fastjson，将Object转换为json字符串;
		// (需要导入com.alibaba.fastjson.JSON包)
		String jsonString = JSON.toJSONString(generalModel);

		StringEntity entity = new StringEntity(jsonString, "UTF-8");

		// post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
		httpPost.setEntity(entity);

		httpPost.setHeader("Content-Type", "application/json;charset=utf8");

		// 响应模型
		CloseableHttpResponse response = null;
		try {
			// 由客户端执行(发送)Post请求
			response = httpClient.execute(httpPost);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();

			String s = EntityUtils.toString(responseEntity);// 转换成字符串

//            log.info("Success:"+Success);
//        	log.info("Message:"+Message);
//        	log.info("Data:"+Data);
//        	log.info("Code:"+Code);
//        	Yc yc = new Yc();
//        	JSONObject jsonObject = JSONObject.fromObject(jsonStr);
//        	Map map = jsonObject;
//        	yc.setData(data);
//        	yc.setCode(Code);

//        	GenOneDataResults gr = JSONObject.toJavaObject(Resultdatas, GenOneDataResults.class);
//        	 log.info("JSON对象转换成Java对象\n" + gr.getSuccess());
//        	 log.info("JSON对象转换成Java对象\n" + gr.getMessage());
//        	 log.info("JSON对象转换成Java对象\n" + gr.getCode());
//        	 log.info("JSON对象转换成Java对象\n" + gr.getData());
//        	

			log.info("响应状态为:" + response.getStatusLine());
			if (responseEntity != null) {
				log.info("响应内容长度为:" + responseEntity.getContentLength());
				log.info("响应内容为:" + s);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	final static String BaseUrl = "http://47.99.64.49:2000";

	public static void main(String[] args) {

		// 请求体通用
		Post8806 t = new Post8806();
		GeneralRequestModel generalRequestModel = new GeneralRequestModel();

		// 44011130197369015P002587 440103301045022105002881 44011130197369015P002586
//	generalRequestModel.setDeviceCode("000000YKDZHJJC0000000420");
		generalRequestModel.setDeviceCode("000000YKDZHJJC0000000542");
		generalRequestModel.setBeginTime("2020-11-11 12:11:15");
		generalRequestModel.setEndTime("2020-11-21 13:11:15");
		generalRequestModel.setPageNumber(2);
		generalRequestModel.setPageNumber(20);
		GenOneDataResults gd = t.AllRequest0086("/Yc/GetHistoryData", generalRequestModel);
		log.info("kaishi:" + generalRequestModel.getDeviceCode() + "的获取8806详细如下");
//	GenOneDataResults gd = t.AllRequest0086("/Yc/GetData",generalRequestModel);
		log.info(gd.getMessage().toString());
		log.info(gd.getCode().toString());
		log.info(gd.getData().toString());
		log.info("----------------以上end");

		generalRequestModel.setDeviceCode("440103301045022105002881");
		generalRequestModel.setBeginTime("2020-11-11 12:11:15");
		generalRequestModel.setEndTime("2020-11-21 13:11:15");
		generalRequestModel.setPageNumber(2);
		generalRequestModel.setPageNumber(20);
		gd = t.AllRequest0086("/Yc/GetHistoryData", generalRequestModel);
		log.info("kaishi:" + generalRequestModel.getDeviceCode() + "的获取8806详细如下");
		log.info(gd.getMessage());
		log.info(gd.getCode().toString());
		log.info(gd.getData().toString());
		log.info("----------------以上end");

		generalRequestModel.setDeviceCode("44011130197369015P002587");
		generalRequestModel.setBeginTime("2020-11-11 12:11:15");
		generalRequestModel.setEndTime("2020-11-21 13:11:15");
		generalRequestModel.setPageNumber(2);
		generalRequestModel.setPageNumber(20);
		gd = t.AllRequest0086("/Yc/GetHistoryData", generalRequestModel);
		log.info("kaishi:" + generalRequestModel.getDeviceCode() + "的获取8806详细如下");
		log.info(gd.getMessage());
		log.info(gd.getCode().toString());
		log.info(gd.getData().toString());
		log.info("----------------以上end");
	}

	/**
	 * 所有对于8806公司的数据请求的封装方法
	 * 请求{"Success":true,"Message":"操作成功","Code":200,"Data":null} 这种情况注意
	 * 
	 * @param AreaUrl
	 * @param generalModel
	 * @return
	 */
	public GenOneDataResults AllRequest0086(String AreaUrl, GeneralRequestModel generalModel) {

		generalModel.setProjectId("6620");

		GenOneDataResults gr = new GenOneDataResults();

		// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		String Url = BaseUrl + AreaUrl;

		// 创建Post请求
		HttpPost httpPost = new HttpPost(Url);

		String jsonString = JSON.toJSONString(generalModel);

		StringEntity entity = new StringEntity(jsonString, "UTF-8");

		// post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
		httpPost.setEntity(entity);

		httpPost.setHeader("Content-Type", "application/json;charset=utf8");

		// 响应模型
		CloseableHttpResponse response = null;
		try {
			// 由客户端执行(发送)Post请求
			response = httpClient.execute(httpPost);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();

			String s = EntityUtils.toString(responseEntity);// 转换成字符串

			if (s == null) {
				log.info("iffanhui");
				return null;
			}
			JSONObject Resultdatas;
			// lj..
//			Map m = new HashMap();
//			m.put("", "");
			try {
				Resultdatas = JSONObject.parseObject(s);// 转换成JSON格式

			} catch (Exception e) {
//				log.info("fastjson处理不了含有空转JSONObject，返回新建对象");
//				gr.setCode(500);
//				gr.setData(m);
//				gr.setSuccess(true);
//				gr.setMessage("null data");
//				return gr;
				return null;

			}
//            log.info(Resultdatas );

//            
//            log.info("Success:"+Success);
//        	log.info("Message:"+Message);
//        	log.info("Data:"+Data);
//        	log.info("Code:"+Code);
//        	Yc yc = new Yc();
//        	JSONObject jsonObject = JSONObject.fromObject(jsonStr);
//        	Map map = jsonObject;
//        	yc.setData(data);
//        	yc.setCode(Code);

			gr = JSONObject.toJavaObject(Resultdatas, GenOneDataResults.class);
//        	 log.info("JSON对象转换成Java对象\n" + gr.getSuccess());
//        	 log.info("JSON对象转换成Java对象\n" + gr.getMessage());
//        	 log.info("JSON对象转换成Java对象\n" + gr.getCode());
//        	 log.info("JSON对象转换成Java对象\n" + gr.getData());

//			log.info("响应状态为:" + response.getStatusLine());
			if (responseEntity != null) {
//				log.info("响应内容长度为:" + responseEntity.getContentLength());
//				log.info("响应内容为:" + s);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		}

		return gr;

	}

//	
	// post 一般参数
//	@Test
//	public void doPostTestFour() {
// 
//		// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
//		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 
//		// 参数
//		StringBuffer params = new StringBuffer();
//		try {
//			// 字符数据最好encoding以下;这样一来，某些特殊字符才能传过去(如:某人的名字就是“&”,不encoding的话,传不过去)
//			params.append("name=" + URLEncoder.encode("&", "utf-8"));
//			params.append("&");
//			params.append("age=24");
//		} catch (UnsupportedEncodingException e1) {
//			e1.printStackTrace();
//		}
// 
//		// 创建Post请求
//		HttpPost httpPost = new HttpPost("http://47.99.64.49:2000/Yc/GetData" + "?" + params);
// 
//		// 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json)
//		httpPost.setHeader("Content-Type", "application/json;charset=utf8");
// 
//		// 响应模型
//		CloseableHttpResponse response = null;
//		try {
//			// 由客户端执行(发送)Post请求
//			response = httpClient.execute(httpPost);
//			// 从响应模型中获取响应实体
//			HttpEntity responseEntity = response.getEntity();
// 
//			log.info("响应状态为:" + response.getStatusLine());
//			if (responseEntity != null) {
//				log.info("响应内容长度为:" + responseEntity.getContentLength());
//				log.info("响应内容为:" + EntityUtils.toString(responseEntity));
//			}
//		} catch (ClientProtocolException e) {
//			e.printStackTrace();
//		} catch (ParseException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			try {
//				// 释放资源
//				if (httpClient != null) {
//					httpClient.close();
//				}
//				if (response != null) {
//					response.close();
//				}
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		}
//	
//	public void doPostTestLLBL() {
//		 
//		// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
//		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 
//		// 创建Post请求
//		HttpPost httpPost = new HttpPost("47.99.64.49:2000/+AreaUrl");
//		// 响应模型
//		CloseableHttpResponse response = null;
//		try {
//			log.info("getEntity:"+httpPost.getEntity());
//			log.info("getConfig:"+httpPost.getConfig());
//			log.info("getReqLine:"+httpPost.getRequestLine());
//			log.info("getURI:"+httpPost.getURI());
//			log.info("getProto:"+httpPost.getProtocolVersion());
//			
//		} catch (Exception e) {
//			log.info("e Message:"+e);
//			// TODO: handle exception
//		}
//		
//		try {
//			// 由客户端执行(发送)Post请求
//			response = httpClient.execute(httpPost);
//			// 从响应模型中获取响应实体
//			HttpEntity responseEntity = response.getEntity();
// 
//			log.info("响应状态为:" + response.getStatusLine());
//			if (responseEntity != null) {
//				log.info("响应内容长度为:" + responseEntity.getContentLength());
//				log.info("响应内容为:" + EntityUtils.toString(responseEntity));
//			}
//		} catch (ClientProtocolException e) {
//			e.printStackTrace();
//		} catch (ParseException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			try {
//				// 释放资源
//				if (httpClient != null) {
//					httpClient.close();
//				}
//				if (response != null) {
//					response.close();
//				}
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//	}

	public static JSONObject getCraneDutyCycle(String deviceCode) {

		Map<String, String> header = new HashMap<String, String>();
		// 0086固定key
		header.put("ApiKey", "cdebdbeedd4f08b2b6f5c223f51c51");

		JSONObject body = new JSONObject();
		// 0086固定的项目编号
		body.put("ProjectId", 6620);
		body.put("DeviceCode", deviceCode);

		HttpRequest request = HttpUtil.createPost("http://47.99.64.49:2000//Tower/GetCycle");

		request.addHeaders(header);
		request.body(body.toJSONString(), "application/json");
		HttpResponse response = request.execute();
		String res = response.body();

		JSONObject resJsonObject = JSONObject.parseObject(res);

		Integer code = resJsonObject.getInteger("Code");

		if (code != 200) {
			return null;
		}

		return resJsonObject.getJSONObject("Data");

	}

}